﻿using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using OnePiece.Core.Extension;
using OnePiece.Core.ModelComponent;

namespace OnePiece.Core.RepositoryComponent
{
    public abstract class RepositoryMapperBase<TEntityObject, TModel>
        where TEntityObject : EntityObject
        where TModel : ModelComponentBase        
    {
        
        public abstract TModel MapToModel(TEntityObject entityObject, TModel injectedModel);

        public abstract TEntityObject MapToEntity(TModel model, TEntityObject injectedEntity);

        public virtual TModel MapToModel(TEntityObject entityObject)
        {
            return MapToModel(entityObject, null);
        }

        public virtual TEntityObject MapToEntity(TModel model)
        {
            return MapToEntity(model, null);
        }

        public virtual IEnumerable<TModel> MapToModel(IEnumerable<TEntityObject> entityObjectList, IEnumerable<TModel> injectedModel)
        {
            
            var entities = (injectedModel  == null )
                                        ? new List<TModel>()
                                        : injectedModel.ToList();

            foreach (var o in entityObjectList)
                entities.Add(MapToModel(o));

            return entities;
        }

        public virtual IEnumerable<TModel> MapToModel(IEnumerable<TEntityObject> entityObjectList)
        {
            return MapToModel(entityObjectList, null);
        }

    }
}
